/**
 * 		-	ad-creatif	-
 * 		-	20/07/2012	-
 *
 * This block is a trap is acting like a pendulum, but it may hurt player and entities. 
 * you can give the speed and range and of course damages amount
 * 
 **/
package blocks {
	import flash.display.Graphics;
	import org.flixel.FlxG;
	
	public class Pendulum extends DynamicBlock {
		
		public var x0:Number;      // Center position
		public var y0:Number;
		public var x1:Number;      // Weight position (center)
		public var y1:Number;
		public var rho:Number;     // Distance from center
		public var theta:Number;   // Angle around center
		public var omega:Number;   // Angular velocity
		public var gravity:Number;
		
		public function Pendulum(X:Number, Y:Number, infos:Object, mapName:String):void {
			super(X,Y,infos,mapName);

			this.x0 = X; 
			this.y0 = Y; 
			this.rho = infos.range * Registry.tileInfos[mapName].tileSize[1];
			this.theta = 70;
			this.omega = 0;
			this.gravity = Registry.gameInfos.world.gravity * (infos.speed) ? infos.speed : 1;
			
			calculatePosition();
			
			// verify if active / alive / exists ...
			exists = true;
			active = true;
			//visible= true;
		}   
		
		public function calculatePosition():void {
			// Rotated 90 degrees, so that 0 is down
			x1 = x0 + Math.sin(theta) * rho;
			y1 = y0 + Math.cos(theta) * rho;
			
			x = x1 - width / 2;
			y = y1 - height / 2;
		}
		
		public function applyGravity():void {
			omega -= FlxG.elapsed * gravity * Math.sin(theta) / rho;
		}
		
		public function applyAngularVelocity():void {
			theta += omega * FlxG.elapsed;
		}
		
		override public function update():void {
			//super.update();
			applyGravity();
			applyAngularVelocity();
			calculatePosition();
			
			//trace(omega);
		}
		
		public function drawString():void {
			
			var gfx:Graphics = FlxG.flashGfx;
			gfx.clear();
			gfx.moveTo(x0, y0);
			gfx.lineStyle(1, 0xFFFF0000);
			gfx.lineTo(x1, y1);
			
			FlxG.camera.buffer.draw(FlxG.flashGfxSprite);
			
		}
		
		override public function draw():void {
		
			//super.draw();
			drawString();
		}
	}
}





/*


package blocks
{
	import blocks.Trap;
	
	import com.greensock.*;
	import com.greensock.data.TweenMaxVars;
	import com.greensock.easing.*;
	
	import flash.events.Event;
	
	import org.flixel.FlxObject;
	import org.flixel.FlxSprite;
	
	import tools.MyFlxSprite;
	
	public class Pendulum extends MyFlxSprite
	{
		private var infos:Object;
		private var pendulum:MyFlxSprite;
		
		public function Pendulum(X:Number, Y:Number, infos:Object, mapName:String){
			super(X,Y);
			
			this.infos= infos;
			
			loadGraphicWithBitmap(infos.image);
			width =  infos.image.width;
			height = infos.image.height;
			origin.x = width/2;
			origin.y = -10;
			
			Registry.dynamicPool.add(this);
			sway(70);
			
		}
		
	
		private function sway(rotation:Number):void{
			if(onScreen()){
				trace("tween dir : ", rotation);
				if(infos.sound) infos.sound.play();
				TweenMax.to(this, 2, {angle:rotation, ease:Circ.easeInOut, onComplete : sway, onCompleteParams : [-rotation]});
			}
		}
	
	}
}
*/